package com.rockcent.api.controller;

import com.rockcent.api.Routes;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.lib.domain.UserFeedback;
import com.rockcent.mall.lib.domain.enumclass.CustomerFeedbackSource;
import com.rockcent.mall.lib.dto.UserFeedbackDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.UserFeedbackRepository;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by DK on 17/3/31.
 */
@RestController
@RequestMapping(value = Routes.API_VERSION)
public class UserFeedbackController extends BaseController{

    private final Logger log = LoggerFactory.getLogger(UserFeedbackController.class);

    @Autowired
    private UserFeedbackRepository userFeedbackRepository;

    @Autowired
    private BaseUserHelper userHelper;

    /**
     * @api {get} /v1/sso/user_feedback/page  用户反馈 分页(R端调用)
     * @apiGroup USER_FEEDBACK
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     *
     *
     * @apiParam {String=ALL("全部"),MERCHANT("商户后台"),MALL("平台后台"), WEBSITE("官网")} source <code>必传参数</code> 账号
     * @apiParam {String} operatorAccount <code>必传参数</code> 提交账号
     *
     *
     * @apiSuccess {long} id 反馈ID
     * @apiSuccess {String=ALL("全部"),MERCHANT("商户后台"),MALL("平台后台"), WEBSITE("官网")} source 反馈来源
     * @apiSuccess {String} operatorAccount 提交账号
     * @apiSuccess {String} feedbackWebsite 反馈页面
     * @apiSuccess {String} feedbackContent 反馈内容
     * @apiSuccess {String} annexeUrl 附件
     * @apiSuccess {date} dateCreated 提交时间
     *
     * @apiSuccessExample {json} Success-Resp：
     * {
     *   "code": "1",
     *   "desc": "成功"
     *   "data" [
     *      {
     *         "id" : 1,
     *         "source" : "WEBSITE",
     *         "dateCreated" : "2016-06-13 18:28:41",
     *         "operatorAccount" : "124412",
     *         "feedbackWebsite" : "一级菜单/二级菜单/三级菜单/四级菜单",
     *         "feedbackContent" : "我是反馈内容",
     *         "annexeUrl" : "http://123.jpg"
     *      },
     *      ........
     *   ]
     * }
     */
    @RequestMapping(value = Routes.SSO_USER_FEEDBACK_PAGE, method = RequestMethod.GET)
    public JsonResult userFeedbackPage (HttpServletRequest request, UserFeedbackDto dto) {
        RestDoing doing = jsonResult -> {
            if (StringUtils.isBlank(dto.getSource())|| !EnumUtils.isValidEnum(CustomerFeedbackSource.class, dto.getSource())) {
                jsonResult.remind("用户反馈来源 ALL(\"全部\"),MERCHANT(\"商户后台\"),MALL(\"平台后台\"), WEBSITE(\"官网\")",log);
                return;
            }
            PageResult<UserFeedbackDto> pageResult = userFeedbackRepository.findUserFeedbackPage(dto.getOperatorAccount(), dto.getSource(), this.getPage(dto.getPageNo(), dto.getPageSize()));
            pageResult.setUrl(Routes.SSO_USER_FEEDBACK_PAGE);
            jsonResult.data = pageResult;
        };
        return doing.go(request, log);
    }

    /**
     * @api {post} /v1/sso/mall_user_feedback/save  用户反馈 保存(M端调用)
     * @apiGroup USER_FEEDBACK
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     *
     *
     * @apiParam {String=MALL("平台后台")} source <code>必传参数</code> 反馈来源
     * @apiParam {String} feedbackWebsite <code>必传参数</code> 反馈页面
     * @apiParam {String} feedbackContent <code>必传参数</code> 反馈内容
     * @apiParam {String} annexeUrl 附件
     *
     * @apiSuccessExample {json} Success-Resp：
     * {
     *   "code": "1",
     *   "desc": "成功"
     * }
     */
    @Transactional
    @RequestMapping(value = Routes.SSO_MALL_USER_FEEDBACK_SAVE, method = RequestMethod.POST)
    public JsonResult mallUserFeedbackSave (HttpServletRequest request, @RequestBody UserFeedback dto) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (StringUtils.isBlank(dto.getFeedbackContent())) {
                jsonResult.remind("反馈内容 不能为空",log);
                return;
            }
            if (dto.getSource() == null || CustomerFeedbackSource.MALL != dto.getSource()) {
                jsonResult.remind("MALL(\"平台后台\")",log);
                return;
            }
            dto.setOperatorAccount(userInfoDto.getUsername());
            if (StringUtils.isBlank(dto.getFeedbackWebsite())) {
                jsonResult.remind("反馈页面  不能为空",log);
            }
            userFeedbackRepository.save(dto);
        };
        return doing.go(request, log);
    }

    /**
     * @api {post} /v1/sso/merchant_user_feedback/save  用户反馈 保存(B端调用)
     * @apiGroup USER_FEEDBACK
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     *
     *
     * @apiParam {String=MERCHANT("商户后台")} source <code>必传参数</code> 反馈来源
     * @apiParam {String} feedbackWebsite <code>必传参数</code> 反馈页面
     * @apiParam {String} feedbackContent <code>必传参数</code> 反馈内容
     * @apiParam {String} annexeUrl 附件
     *
     * @apiSuccessExample {json} Success-Resp：
     * {
     *   "code": "1",
     *   "desc": "成功"
     * }
     */
    @Transactional
    @RequestMapping(value = Routes.SSO_MERCHANT_USER_FEEDBACK_SAVE, method = RequestMethod.POST)
    public JsonResult merchantUserFeedbackSave (HttpServletRequest request, @RequestBody UserFeedback dto) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (StringUtils.isBlank(dto.getFeedbackContent())) {
                jsonResult.remind("反馈内容 不能为空",log);
                return;
            }
            if (dto.getSource() == null || CustomerFeedbackSource.MERCHANT != dto.getSource()) {
                jsonResult.remind("MALL(\"平台后台\")",log);
                return;
            }
            dto.setOperatorAccount(userInfoDto.getUsername());
            if (StringUtils.isBlank(dto.getFeedbackWebsite())) {
                jsonResult.remind("反馈页面  不能为空",log);
            }
            userFeedbackRepository.save(dto);
        };
        return doing.go(request, log);
    }


    /**
     * @api {post} /v1/sso/site_user_feedback/save  用户反馈 保存(官网调用)
     * @apiGroup USER_FEEDBACK
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     *
     *
     * @apiParam {String=WEBSITE("官网")} source <code>必传参数</code> 反馈来源
     * @apiParam {String} feedbackWebsite <code>必传参数</code> 反馈页面
     * @apiParam {String} feedbackContent <code>必传参数</code> 反馈内容
     * @apiParam {String} annexeUrl 附件
     *
     * @apiSuccessExample {json} Success-Resp：
     * {
     *   "code": "1",
     *   "desc": "成功"
     * }
     */
    @Transactional
    @RequestMapping(value = Routes.SSO_SITE_USER_FEEDBACK_SAVE, method = RequestMethod.POST)
    public JsonResult siteUserFeedbackSave (HttpServletRequest request, @RequestBody UserFeedback dto) {
        RestDoing doing = jsonResult -> {
            if (StringUtils.isBlank(dto.getFeedbackContent())) {
                jsonResult.remind("反馈内容 不能为空",log);
                return;
            }
            if (dto.getSource() == null || CustomerFeedbackSource.WEBSITE != dto.getSource()) {
                jsonResult.remind("MALL(\"平台后台\")",log);
                return;
            }

            if (StringUtils.isBlank(dto.getFeedbackWebsite())) {
                jsonResult.remind("反馈页面  不能为空",log);
            }
            userFeedbackRepository.save(dto);
        };
        return doing.go(request, log);
    }
}
